package com.pet.server.ext.business;

import java.util.ArrayList;
import java.util.List;

import com.pet.server.ext.ApiError;
import com.pet.server.ext.constants.Status.Source;
import com.pet.server.ext.data.Campaign;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.service.TbkService;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * 商城首页
 * */
public class MallBusiness {
	
	
	public static JSONObject JsonData(List<Campaign> banner,List<Constant> categoryList) throws ApiError{
		UserService userService = new UserService();
		TbkService tbkService = new TbkService();
		
		JSONObject o = new JSONObject();
        JSONArray bannerJson = new JSONArray();
		for(Campaign cam:banner){
			JSONObject co = new JSONObject();
	 		co.put("click_type", cam.getClick_type());
	 		co.put("click_url", cam.getClick_url());
	 		co.put("text", cam.getText());
	 		co.put("img_url", cam.getImg_url());
	 		co.put("targt", cam.getTarget());
	 		co.put("param", StringUtil.dealEmpty(cam.getParam(), null));
	 		
            bannerJson.add(co);
		}
        o.put("banner", bannerJson);
        
        
        JSONArray listJson = new JSONArray();
        
        for(Constant c:categoryList){
        	JSONObject so = new JSONObject();
        	so.put("category_name", c.conts_name);
        	so.put("category_type", c.code);
        	so.put("ui_style", c.val);
        	
        	//append banner json
        	List<Campaign> clist = userService.queryCampaign(c.getCode(), 3);
        	JSONArray baner = new JSONArray();
    		for(Campaign cam:clist){
    			JSONObject co = new JSONObject();
    	 		co.put("click_type", cam.getClick_type());
    	 		co.put("click_url", cam.getClick_url());
    	 		co.put("text", cam.getText());
    	 		co.put("img_url", cam.getImg_url());
    	 		co.put("targt", cam.getTarget());
    	 		co.put("param", StringUtil.dealEmpty(cam.getParam(),null));
    	 		baner.add(co);
    		}
    		so.put("banner", baner);
    		
    		
    		//append product json
    		List<ProductInfo> products = tbkService.queryTbkProductInfo(null, c.code, null, null, null, null, 8);
    		JSONArray productJson = new JSONArray();
    		for(ProductInfo p:products){
    			JSONObject po = new JSONObject();
    			po.put("ico_path", p.ico_path);
    			po.put("id", p.id);
    			po.put("pet_cate", p.pet_cate);
    			po.put("product_cate", p.product_cate);
    			//po.put("product_description", "");
    			po.put("product_href", p.product_href);
    			po.put("product_name", p.product_name);
    			po.put("product_price", p.product_price);
    			po.put("sale_price", p.sale_price);
    			po.put("source", Source.tmall.toString());
    			if(!StringUtil.isEmpty(p.images)&&!"null".equals(p.images)){
    				//po.put("images", p.images);
    			}
    			
    			//System.out.println(p.num_iid+"=============================");
    			
    			po.put("num_iid", p.num_iid);
    			productJson.add(po);
    		}
    		
    		so.put("products",productJson);
    		
    		listJson.add(so);
    		
        }
        
		o.put("list", listJson);
		return o;
	}
	
	public static List<Campaign> queryCampaignByConstantbak(Constant c){
		List<Campaign> list = new ArrayList<>();
		Campaign campaign = new Campaign();
		campaign.click_type="webview";
		campaign.click_url="http://h5.m.jd.com/active/4Fnzyov43q1BpJMhYPtKgXPRhALM/index.html";
		campaign.param="";
		campaign.img_url="http://img13.360buyimg.com/cms/jfs/t2794/87/4112033500/44806/67998b51/57a7fde1N0437b4ad.jpg!q35.jpg";
		campaign.text="看奥运抢好货";
		list.add(campaign);
		return list;
	}
	
	public static List<Campaign> queryCampaignByConstant(Constant c) throws ApiError{
		UserService userService = new UserService();
		return userService.queryCampaign(c.getCode(), 3);
	}
	
	
	
	
	public static List<ProductInfo> queryProductByConstant(Constant c){
		List<ProductInfo> list = new ArrayList<>();
		list.add(newsP());
		list.add(newsP());
		list.add(newsP());
		list.add(newsP());
		list.add(newsP());
		list.add(newsP());
		list.add(newsP());
		list.add(newsP());
		list.add(newsP());
		return list;
	}
	
	public static ProductInfo newsP(){
		ProductInfo p = new ProductInfo();
		p.ico_path="http://img12.360buyimg.com/n7/jfs/t2872/45/2866612544/262405/fea8d4c7/577637bcNd5549555.jpg";
		p.id=83;
		p.product_name="美赞臣（MeadJohnson）奶粉 港版2段900g（适用于6-12个月宝宝） 荷兰原罐";
		p.product_price=254d;
		p.sale_price=254d;
		p.product_href="http://m.jd.hk/product/2210565.html?sid=0bc91e8d0777972c7193a949c79c8bbd";
		p.pet_cate="001";
		p.product_cate="001002";
		return p;
	}
	
	public static Campaign newsCam(){
		Campaign campaign = new Campaign();
		campaign.click_type="webview";
		campaign.click_url="http://h5.m.jd.com/active/4Fnzyov43q1BpJMhYPtKgXPRhALM/index.html";
		campaign.param="";
		campaign.img_url="http://img13.360buyimg.com/cms/jfs/t2794/87/4112033500/44806/67998b51/57a7fde1N0437b4ad.jpg!q35.jpg";
		campaign.text="看奥运抢好货";
		return campaign;
	}
	
	
		
	
	
	
}
